/*
 * QWindowsClassicStyle.cpp  0.4.0 / Nov 17, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
#include "QWindowsClassicStyle.h"

// TODO: clean - make better

#include <QApplication>
#include <QFrame>
#include <QPainter>
#include <QPolygon>
#include <QStyleOption>

// QWindowsClassicStyle :: Public Constructor. ---------------------------------

/*!
 * \brief Constructs new QWindowsClassicStyle object.
 */
QWindowsClassicStyle::QWindowsClassicStyle()
  : QWindowsStyle(), m_patchDisabled(false)
{
  // empty constructor body
}

// QWindowsClassicStyle :: Overridden Member Functions. ------------------------

/*!
 * \inheritDoc
 */
int QWindowsClassicStyle::pixelMetric(PixelMetric metric,
                                      const QStyleOption *option,
                                      const QWidget *widget) const
{
  if (m_patchDisabled) {
    return QWindowsStyle::pixelMetric(metric, option, widget);
  }
  switch (metric) {
    case PM_MDIMinimizedWidth:
      return QWindowsStyle::pixelMetric(metric, option, widget) - 20;
    case PM_DefaultFrameWidth:
      return 2;
    case PM_ToolBarHandleExtent:
      return 10;
    case PM_ToolBarItemSpacing:
      return 0;
    case PM_ToolBarItemMargin:
      return 1;
    case PM_MenuHMargin:
      return 0;
    case PM_MenuVMargin:
      return 0;
    case PM_TitleBarHeight:
      return QCommonStyle::pixelMetric(metric, option, widget);
    case PM_HeaderGripMargin:
      return 2;
    case PM_HeaderMargin:
      return 2;
    case PM_HeaderMarkSize:
      return 16;
    default:
      return QWindowsStyle::pixelMetric(metric, option, widget);
  }
}

/*!
 * \inheritDoc
 */
void QWindowsClassicStyle::drawPrimitive(PrimitiveElement element,
                                          const QStyleOption *option,
                                          QPainter *painter,
                                          const QWidget *widget) const
{
  if (m_patchDisabled) {
    QWindowsStyle::drawPrimitive(element, option, painter, widget);
    return;
  }
  switch (element) {
    case PE_IndicatorToolBarHandle:
      painter->save();
      painter->translate(option->rect.x(), option->rect.y());
      if (option->state & State_Horizontal) {
        int x;
        if (QApplication::layoutDirection() == Qt::RightToLeft) {
          x = option->rect.width() - 1;
        } else {
          x = 2;
        }
        if (option->rect.height() > 4) {
          qDrawShadePanel(painter, x, 2, 3, option->rect.height() - 4,
                          option->palette, false, 1, 0);
        }
      } else {
        if (option->rect.width() > 4) {
          qDrawShadePanel(painter, 2, 2, option->rect.width() - 4, 3,
                          option->palette, false, 1, 0);
        }
      }
      painter->restore();
      break;
    default:
      QWindowsStyle::drawPrimitive(element, option, painter, widget);
      break;
  }
}

/*!
 * \inheritDoc
 */
void QWindowsClassicStyle::drawComplexControl(ComplexControl control,
                                              const QStyleOptionComplex *option,
                                              QPainter *painter,
                                              const QWidget *widget) const
{
  if (m_patchDisabled) {
    QWindowsStyle::drawComplexControl(control, option, painter, widget);
    return;
  }
  switch(control) {
    case CC_TitleBar:
      if (const QStyleOptionTitleBar *tb =
                     qstyleoption_cast<const QStyleOptionTitleBar *>(option)) {
        QStyleOptionTitleBar tbo = *tb;
        QPalette palette = QApplication::palette();
        const QColor highlight = palette.color(QPalette::Active,
                                               QPalette::Highlight);
        if (highlight != m_leftTitleBarColor) {
          m_leftTitleBarColor = highlight;
          m_rightTitleBarColor = highlight.lighter(340);
          m_leftTitleBarInactiveColor = palette.color(QPalette::Active,
                                               QPalette::Dark);
          m_rightTitleBarInactiveColor =
                                       m_leftTitleBarInactiveColor.lighter(160);
        }
        tbo.palette.setColor(QPalette::Active, QPalette::Highlight,
                             m_leftTitleBarColor);
        tbo.palette.setColor(QPalette::Active, QPalette::Base,
                             m_rightTitleBarColor);
        tbo.palette.setColor(QPalette::Inactive, QPalette::Highlight,
                             m_leftTitleBarInactiveColor);
        tbo.palette.setColor(QPalette::Inactive, QPalette::Base,
                             m_rightTitleBarInactiveColor);
        tbo.palette.setColor(QPalette::All, QPalette::HighlightedText,
                             QApplication::palette().highlightedText().color());
        QFont font = QApplication::font();
        font.setBold(true);
        painter->setFont(font);
        QWindowsStyle::drawComplexControl(control, &tbo, painter, widget);
        return;
      }
    default:
      QWindowsStyle::drawComplexControl(control, option, painter, widget);
      return; 
  }
}

/*!
 * \inheritDoc
 */
int QWindowsClassicStyle::styleHint(StyleHint hint, const QStyleOption *option,
                                    const QWidget *widget,
                                    QStyleHintReturn *returnData) const
{
  if (m_patchDisabled) {
    return QWindowsStyle::styleHint(hint, option, widget, returnData);
  }
  switch(hint) {
    case SH_ComboBox_PopupFrameStyle:
      return QFrame::Panel | QFrame::Plain;
    default:
      return QWindowsStyle::styleHint(hint, option, widget, returnData);
  }
}

/*!
 * \inheritDoc
 */
QRect QWindowsClassicStyle::subElementRect(SubElement sr,
                                           const QStyleOption *opt,
                                           const QWidget *widget) const
{
  if (m_patchDisabled) {
    return QWindowsStyle::subElementRect(sr, opt, widget);
  }
  switch (sr) {
    case SE_HeaderLabel: {
      QRect r = QWindowsStyle::subElementRect(sr, opt, widget);
      return r;
    }
    default:
      return QWindowsStyle::subElementRect(sr, opt, widget);
  }
}

/*!
 * \inheritDoc
 */
QPalette QWindowsClassicStyle::standardPalette() const
{
  QPalette palette = QWindowsStyle::standardPalette();
  palette.setColor(QPalette::All, QPalette::Highlight, QColor(20, 46, 106));
  return palette;
}    
